﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using AdverseEventReportSystem.Service;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.AdverseEvent
{
    public class AdverseEventReviewController : BaseController
    {
        // GET: AdverseEvent/AdverseEventReview
        public ActionResult Index()
        {
            ViewBag.Status = WebUtils.GetString("status");
            ViewBag.DateType = WebUtils.GetString("dateType");
            ViewBag.CurrDepCode = loginUser.CurrDepCode;
            return View();
        }

        /// <summary>
        /// 事件列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            int pageIndex = DataConvert.StrToInt(Request["page"], 1);
            int pageSize = DataConvert.StrToInt(Request["pageSize"], ConfigHelper.GetConfigInt("pagesize"));
            string whereSql = GetWhereSql();
            SelectBuilder data = new SelectBuilder();
            data.Having = "";
            data.GroupBy = "";
            data.OrderBy = "ID DESC";
            data.From = "AdverseEvent";
            data.Select = "ID,EventTypeName,NurseName,AreaName,OccurrTime,CreateTime,Status,Flag_Yc,FanKuiState,PatientName,PatientID,PatientInTime,TransferTime,ReviewTime,(SELECT Value FROM AdverseEventModeRecord(NOLOCK) WHERE EventID = AdverseEvent.ID AND ModeName ='压伤类型') AS YsType";
            data.WhereSql = whereSql;
            data.PagingCurrentPage = pageIndex;
            data.PagingItemsPerPage = pageSize;
            int total = 0;
            DataTable dt = BaseService<Model.AdverseEvent>.GetQueryManyForPage_Proc(data, out total);
            AdverseEventManager.GetYsType(dt);
            var result = new { Total = total, Rows = dt };
            return JsonDate(result);
        }

        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        private string GetWhereSql()
        {
            int year = DataConvert.StrToInt(Request["year"]);
            int quarter = DataConvert.StrToInt(Request["quarter"]);
            int month = DataConvert.StrToInt(Request["month"]);
            int eventTypeID = DataConvert.StrToInt(Request["eventTypeID"]);
            int status = DataConvert.StrToInt(Request["status"]);
            string areaID = DataConvert.ToString(Request["areaID"]);

            string whereSql = "";
            if (areaID == "-1")
            {
                whereSql += "AreaID in ('" + string.Join("','", loginUser.DepartCode.Split('&')) + "')";
            }
            else
            {
                whereSql += string.Format(" AreaID = '{0}'", areaID);
            }
            if (status != -1)
            {
                whereSql += string.Format(" and Status = {0}", status);
            }
            else
            {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                ReviewProcess reviewProcess = new ReviewProcess();
                reviewProcess = XmlHelper.ReadXML<ReviewProcess>(Server.MapPath("~/") + "Xml\\AdverseEvent\\ReviewProcess.xml", reviewProcess);
                foreach (var item in reviewProcess.Jobs)
                {
                    if (item.Title == loginUser.JobTitle)
                    {
                        whereSql += string.Format(" and Status in ({0})", string.Join(",", item.IncludeStatus.Split('|')));
                        break;
                    }
                }
            }
            if (quarter == 0)
            {
                if (month == 0)
                {
                    whereSql += string.Format(" and CreateTime >= '{0}' and CreateTime < '{1}'", new DateTime(year, 1, 1).ToString(), new DateTime(year, 1, 1).AddYears(1).ToString());
                }
                else
                {
                    whereSql += string.Format(" and CreateTime >= '{0}' and CreateTime < '{1}'", new DateTime(year, month, 1).ToString(), new DateTime(year, month, 1).AddMonths(1).ToString());
                }
            }
            else
            {
                if (month == 0)
                {
                    whereSql += string.Format(" and CreateTime >= '{0}' and CreateTime < '{1}'", new DateTime(year, quarter * 3 - 2, 1).ToString(), new DateTime(year, quarter * 3 - 2, 1).AddMonths(3).ToString());
                }
                else
                {
                    whereSql += string.Format(" and CreateTime >= '{0}' and CreateTime < '{1}'", new DateTime(year, month, 1).ToString(), new DateTime(year, month, 1).AddMonths(1).ToString());
                }
            }
            if (eventTypeID > 0)
            {
                whereSql += string.Format(" and EventTypeID = {0}", eventTypeID);
            }

            return whereSql;
        }

        /// <summary>
        /// 审核页面 
        /// GET: AdverseEvent/AdverseEventReview/Review
        /// </summary>
        /// <returns></returns>
        public ActionResult Review()
        {
            return View();
        }

        /// <summary>
        /// 审核不良事件
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <param name="oldStatus"></param>
        /// <param name="duDaoRemark"></param>
        /// <param name="reviewReason"></param>
        /// <param name="yuHou"></param>
        /// <param name="zhuanGui"></param>
        /// <param name="adverseEventInfo"></param>
        /// <returns></returns>
        public ActionResult ReviewSave(int id, int status, int oldStatus, string duDaoRemark, string reviewReason, string yuHou, string zhuanGui, AdverseEventInfo adverseEventInfo)
        {
            OperateResult resultInfo = AdverseEventManager.Review(id, status, oldStatus, duDaoRemark, reviewReason, yuHou, zhuanGui, adverseEventInfo);
            return JsonDate(resultInfo);
        }


        /// <summary>
        /// 获取登录用户配置的审核权限
        /// </summary>
        /// <returns></returns>
        public ActionResult GetReviewProcess()
        {
            Job job = AdverseEventManager.GetReviewProcess("AdverseEvent\\ReviewProcess.xml");
            return JsonDate(job);
        }
    }
}